package br.faimg.pomar.visual;

import br.faimg.pomar.controle.BusinessDelegate.ProducaoBusinessDelegate;
import br.faimg.pomar.controle.BusinessDelegate.TipodeErroBusinessDelegate;
import java.awt.event.KeyEvent;

/*
 * CadastroDefeito.java
 *
 * Created on 17 de Agosto de 2008, 14:28
 */
/**
 *
 * @author  diogo
 */
public class CadastroReparo extends javax.swing.JDialog {

    private String numerodeSerie;
    private String codigoEstacao;
    private String codigoReparo;

    /** Creates new form CadastroDefeito */
    public CadastroReparo(String numerodeserie, String codigoestacao) {
        initComponents();

        TipodeErroBusinessDelegate delegate = new TipodeErroBusinessDelegate();
        descricaoDefeitpojTextArea.insert(delegate.readUltimoDefeito(numerodeserie).getDescricao(), 0);

        numerodeSerie = numerodeserie;
        codigoEstacao = codigoestacao;
        
        codigoReparo = "";
        
        ConfirmarjButton.setEnabled(false);

    }
    
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        codigodeReparojTextField = new javax.swing.JTextField();
        ConfirmarjButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        descricaoReparojTextArea = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        descricaoDefeitpojTextArea = new javax.swing.JTextArea();
        cancelarjButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Informe de defeito");
        setAlwaysOnTop(true);
        setMinimumSize(new java.awt.Dimension(400, 200));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setText("DESCRIÇÃO DO DEFEITO");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        jLabel1.setText("DESCRIÇÃO DO REPARO");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, -1, -1));

        codigodeReparojTextField.setFont(new java.awt.Font("UnDotum", 1, 18));
        codigodeReparojTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                codigodeReparojTextFieldKeyPressed(evt);
            }
        });
        jPanel1.add(codigodeReparojTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 180, 260, 40));

        ConfirmarjButton.setText("Confirmar");
        ConfirmarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConfirmarjButtonActionPerformed(evt);
            }
        });
        jPanel1.add(ConfirmarjButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 230, 260, 30));

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        jScrollPane1.setFocusable(false);

        descricaoReparojTextArea.setBackground(new java.awt.Color(9, 172, 1));
        descricaoReparojTextArea.setColumns(20);
        descricaoReparojTextArea.setEditable(false);
        descricaoReparojTextArea.setFont(new java.awt.Font("Arial Black", 2, 18)); // NOI18N
        descricaoReparojTextArea.setForeground(new java.awt.Color(254, 254, 254));
        descricaoReparojTextArea.setRows(5);
        descricaoReparojTextArea.setFocusable(false);
        jScrollPane1.setViewportView(descricaoReparojTextArea);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 180, 440, 120));

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        jScrollPane2.setFocusable(false);

        descricaoDefeitpojTextArea.setBackground(new java.awt.Color(255, 0, 29));
        descricaoDefeitpojTextArea.setColumns(20);
        descricaoDefeitpojTextArea.setEditable(false);
        descricaoDefeitpojTextArea.setFont(new java.awt.Font("Arial Black", 2, 18));
        descricaoDefeitpojTextArea.setForeground(new java.awt.Color(254, 254, 254));
        descricaoDefeitpojTextArea.setRows(5);
        descricaoDefeitpojTextArea.setFocusable(false);
        jScrollPane2.setViewportView(descricaoDefeitpojTextArea);

        jPanel1.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 720, 100));

        cancelarjButton.setText("Cancelar");
        cancelarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarjButtonActionPerformed(evt);
            }
        });
        jPanel1.add(cancelarjButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 270, 260, 30));

        jLabel2.setText("CÓDIGO DE REPARO");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 160, -1, -1));
        jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 140, 720, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 740, 330));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void codigodeReparojTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_codigodeReparojTextFieldKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {

        if (codigodeReparojTextField.getText().startsWith("S") || codigodeReparojTextField.getText().startsWith("R")) {
           
            TipodeErroBusinessDelegate delegate = new TipodeErroBusinessDelegate();
            descricaoReparojTextArea.insert(delegate.readById(codigodeReparojTextField.getText()).getDescricao(), 0);
            ConfirmarjButton.setEnabled(true);
            ConfirmarjButton.requestFocus();
            
            
            codigodeReparojTextField.setEnabled(false);
            
        }  else {
            
            ErrorView view = new ErrorView("Digite um código de erro válido!");
            view.setModal(true);
            view.setLocationRelativeTo(null);
            view.setVisible(true);
            codigodeReparojTextField.setText("");
        }
        
        
    }
}//GEN-LAST:event_codigodeReparojTextFieldKeyPressed

private void ConfirmarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConfirmarjButtonActionPerformed

    //Inserir o codigo de validacao do produto
    ProducaoBusinessDelegate delegate = new ProducaoBusinessDelegate();

    //leitura de um código de reparo
    if (codigodeReparojTextField.getText().startsWith("R") || codigodeReparojTextField.getText().startsWith("S")) {

        codigoReparo = codigodeReparojTextField.getText();
        
        try {
            delegate.createProdutoComReparo(numerodeSerie, codigoReparo, codigoEstacao);

            this.setVisible(false);

        } catch (Throwable e) {
            ErrorView view = new ErrorView(e.toString());
            view.setModal(true);
            view.setLocationRelativeTo(null);
            view.setVisible(true);
            
            codigodeReparojTextField.setText("");
            
            descricaoReparojTextArea.setText("");
            
            codigodeReparojTextField.setEnabled(true);
        
            codigodeReparojTextField.requestFocus(true);
        }
    }

}//GEN-LAST:event_ConfirmarjButtonActionPerformed

private void cancelarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarjButtonActionPerformed

    if (!codigodeReparojTextField.getText().equals("")){
        
        codigodeReparojTextField.setEnabled(true);
        codigodeReparojTextField.setText("");
        descricaoReparojTextArea.setText("");
        
    }else{
    
        this.setVisible(false);
    }
    
}//GEN-LAST:event_cancelarjButtonActionPerformed
    /**
     * @param args the command line arguments
     */
//Comentado para testes
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new CadastroDefeito().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ConfirmarjButton;
    private javax.swing.JButton cancelarjButton;
    private javax.swing.JTextField codigodeReparojTextField;
    private javax.swing.JTextArea descricaoDefeitpojTextArea;
    private javax.swing.JTextArea descricaoReparojTextArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables

    public String getCodigoReparo() {
        return codigoReparo;
    }
}
